home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Mac Source / FileUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-10  |  6.2 KB  |  223 lines  |  [TEXT/CWIE]

  1. /*
  2. ** File:        FileUtils.c
  3. **
  4. ** Written by:    Bill Hayden
  5. **                Nikol Software
  6. **
  7. ** Copyright Â© 1995 Nikol Software
  8. ** All rights reserved.
  9. */
  10.  
  11.  
  12. #include "FileUtils.h"
  13. #include "StringUtils.h"
  14.  
  15.  
  16.  
  17. /*****************************************************************************/
  18.  
  19.  
  20. Boolean    SelectWorldFile(Str255    theFileName)
  21. {
  22.     SFTypeList            sfList = {'TEXT'};
  23.     StandardFileReply    sfReply;
  24.  
  25.     StandardGetFile(nil, 1, sfList, &sfReply);
  26.  
  27.     if (sfReply.sfGood)
  28.         {
  29.         PathNameFromDirID(sfReply.sfFile.vRefNum, sfReply.sfFile.parID, theFileName);
  30.         pcat(theFileName, sfReply.sfFile.name);
  31.         }
  32.         
  33.     return sfReply.sfGood;
  34. }
  35.  
  36.  
  37. /*****************************************************************************/
  38.  
  39.  
  40. OSErr PathNameFromDirID(short vRefNum, long dirID, Str255 fullPathName)
  41. {
  42.     Str255        dirName;
  43.     DirInfo        dirInf;
  44.     OSErr        err;
  45.  
  46.     *fullPathName = 0;
  47.  
  48.     dirInf.ioNamePtr = dirName;
  49.     dirInf.ioDrParID = dirID;
  50.  
  51.     do {
  52.         dirInf.ioVRefNum = vRefNum;
  53.         dirInf.ioFDirIndex = -1;                 // -1 means use ioDrDirID…
  54.         dirInf.ioDrDirID = dirInf.ioDrParID;
  55.  
  56.         err = PBGetCatInfoSync((CInfoPBPtr)&dirInf);
  57.  
  58.         if (err == noErr)
  59.             {
  60.             dirName[++dirName[0]] = ':';
  61.  
  62.             if (dirName[0] + fullPathName[0] > 255)
  63.                 err = bdNamErr;                 // too big to eat!
  64.             else
  65.                 {
  66.                 pcat(dirName, fullPathName);
  67.                 pcpy(fullPathName, dirName);
  68.                 }
  69.             }
  70.  
  71.     } while (dirInf.ioDrDirID != fsRtDirID && err == noErr);
  72.  
  73.     return err;
  74.  
  75. }    // PathNameFromDirID
  76.  
  77.  
  78. /*****************************************************************************/
  79.  
  80.  
  81. OSErr    GetFilenameFromPathname(ConstStr255Param pathname, Str255 filename)
  82. {
  83.     short    index;
  84.     short    nameEnd;
  85.  
  86.     /* default to no filename */
  87.     filename[0] = 0;
  88.  
  89.     /* check for no pathname */
  90.     if ( pathname == NULL )
  91.         return ( notAFileErr );
  92.     
  93.     /* get string length */
  94.     index = pathname[0];
  95.     
  96.     /* check for empty string */
  97.     if ( index == 0 )
  98.         return ( notAFileErr );
  99.     
  100.     /* skip over last trailing colon (if any) */
  101.     if ( pathname[index] == ':' )
  102.         --index;
  103.  
  104.     /* save the end of the string */
  105.     nameEnd = index;
  106.  
  107.     /* if pathname ends with multiple colons, then this pathname refers */
  108.     /* to a directory, not a file */
  109.     if ( pathname[index] == ':' )
  110.         return ( notAFileErr );
  111.         
  112.     
  113.     /* parse backwards until we find a colon or hit the beginning of the pathname */
  114.     while ( (index != 0) && (pathname[index] != ':') )
  115.     {
  116.         --index;
  117.     }
  118.     
  119.     /* if we parsed to the beginning of the pathname and the pathname ended */
  120.     /* with a colon, then pathname is a full pathname to a volume, not a file */
  121.     if ( (index == 0) && (pathname[pathname[0]] == ':') )
  122.         return ( notAFileErr );
  123.     
  124.     /* get the filename and return noErr */
  125.     filename[0] = (char)(nameEnd - index);
  126.     BlockMoveData(&pathname[index+1], &filename[1], nameEnd - index);
  127.     return ( noErr );
  128. }
  129.  
  130.  
  131.  
  132. /*****************************************************************************/
  133.  
  134.  
  135.  
  136. // strongly based on KillEveryoneButMe.c by C. K. Haun of Apple Computer
  137.  
  138. void QuitEverythingButMe(void)
  139. {
  140.     ProcessSerialNumber    myProc, processSN;
  141.     ProcessSerialNumber    finderPSN;
  142.     ProcessInfoRec        infoRec;
  143.     Str31                processName;
  144.     FSSpec                procSpec;
  145.     OSErr                myErr = noErr;
  146.     OSErr                otherError;
  147.     AppleEvent            theEvent;
  148.     AEDesc                theAddress;
  149.     Boolean                ourFlag, notFinder;
  150.     Boolean                finderFound = false;
  151.     
  152.     
  153.     GetCurrentProcess(&myProc);
  154.     /* Preset the PSN to no PSN, see IM VI, the Process Manager */
  155.     processSN.lowLongOfPSN = kNoProcess;
  156.     processSN.highLongOfPSN = kNoProcess;
  157.     finderPSN.lowLongOfPSN = nil;
  158.     finderPSN.highLongOfPSN = nil;
  159.     
  160.     do {
  161.         myErr = GetNextProcess(&processSN);
  162.         SameProcess(&myProc, &processSN, &ourFlag);
  163.         if (!ourFlag && !finderFound)
  164.             {
  165.             /* see if it's the Finder, we have to kill the finder LAST */
  166.             /* or else non-sys 7 apps won't get killed */
  167.             /* since the Finder must be there to convert the AppleEvent to Puppet Strings */
  168.             /* if the app is not APpleEvent aware */
  169.             infoRec.processInfoLength = sizeof(ProcessInfoRec);
  170.             infoRec.processName = processName;
  171.             infoRec.processAppSpec = &procSpec;
  172.             GetProcessInformation(&processSN, &infoRec);
  173.             if (infoRec.processSignature == 'MACS' && infoRec.processType == 'FNDR')
  174.                 {
  175.                 /* save this number for later  */
  176.                 finderPSN = processSN;
  177.                 notFinder = false;
  178.                 }
  179.             else
  180.                 {
  181.                 notFinder = true;
  182.                 finderFound = true;
  183.                 }
  184.             }
  185.             
  186.         if (!myErr && !ourFlag && notFinder)
  187.             {
  188.             otherError = AECreateDesc(typeProcessSerialNumber, (Ptr)&processSN, sizeof(processSN), &theAddress);
  189.             if (!otherError)
  190.                 otherError = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &theAddress, kAutoGenerateReturnID,
  191.                                                 kAnyTransactionID, &theEvent);
  192.             if (!otherError)
  193.                 AEDisposeDesc(&theAddress);
  194.             /* Again, the Finder will convert the AppleEvent to puppetstrings if */
  195.             /* the application is a System 6 or non-AE aware app.  This ONLY  */
  196.             /* happens for the 4 required (oapp,odoc,pdoc, and quit) AppleEvents  */
  197.             /* and ONLY if you use the PSN for the address */
  198.             if (!otherError)
  199.                 AESend(&theEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout,
  200.                        nil, nil);
  201.             AEDisposeDesc(&theEvent);
  202.             }
  203.     } while (!myErr);
  204.     
  205.     /* Now, if the finder was running, it's safe to kill it */
  206.     if (finderPSN.lowLongOfPSN || finderPSN.highLongOfPSN)
  207.         {
  208.         otherError = AECreateDesc(typeProcessSerialNumber, (Ptr)&finderPSN, sizeof(processSN), &theAddress);
  209.         if (!otherError)
  210.             otherError = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &theAddress, kAutoGenerateReturnID,
  211.                                             kAnyTransactionID, &theEvent);
  212.         if (!otherError)
  213.             AEDisposeDesc(&theAddress);
  214.             
  215.         if (!otherError)
  216.             AESend(&theEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil,
  217.                    nil);
  218.                    
  219.         AEDisposeDesc(&theEvent);
  220.         }
  221. }
  222.  
  223.